package com.example.zxw.ui.news.newstabpage

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.zxw.entity.ChannelEntity
import com.example.zxw.ui.items.NewItemCompose
import com.example.zxw.ui.views.RequestDataStatus
import com.example.zxw.ui.views.RequestDataStatus.RefreshIng
import com.example.zxw.ui.views.smartrefresh.CnsSmartRefresh
import com.fmt.mvi.learn.gobal.log
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 通用的新闻tab页
 */
@Composable
fun CommentNewsTabPage(channelEntity: ChannelEntity, isShow: Boolean) {
    "CommentNewsTabPage   $isShow ".log()
    val viewModel: CommentNewsTabPageViewModel = viewModel()
    val uiState = viewModel.uiState.collectAsState()
    LaunchedEffect(key1 = isShow, block = {
        if (uiState.value.datas == null && isShow) {
            viewModel.loadData(channelEntity.cname)
        }
    })
    Box(modifier = Modifier.fillMaxSize()) {
        "CommentNewsTabPage box ${uiState.value.status}".log()
        CnsSmartRefresh(isFreshing = uiState.value.status == RefreshIng, onRefresh = {
            viewModel.loadData(channelEntity.cname)
        }, content = {
            LazyColumn(content = {
                uiState.value.datas?.forEach {
                    item {
                        NewItemCompose(it)
                    }
                }
                if (uiState.value.status == RequestDataStatus.LoadMoreFail) {
                    item {
                        Box(
                            modifier = Modifier
                                .fillMaxWidth()
                                .align(Alignment.Center)
                        ) {
                            Text(
                                text = "加载失败",
                                modifier = Modifier
                                    .align(Alignment.Center)
                                    .padding(2.dp)
                                    .wrapContentSize()
                            )
                        }
                    }
                } else if (uiState.value.status == RequestDataStatus.NoMoreData) {
                    item {
                        Box(
                            modifier = Modifier
                                .fillMaxWidth()
                                .align(Alignment.Center)
                        ) {
                            Text(
                                text = "没有更多数据",
                                modifier = Modifier
                                    .align(Alignment.Center)
                                    .padding(2.dp)
                                    .wrapContentSize()
                            )
                        }
                    }
                }else if(uiState.value.datas!=null){
                    item {
                        Box(
                            modifier = Modifier
                                .fillMaxWidth()
                                .align(Alignment.Center)
                        ) {
                            Text(
                                text = "1加载中...",
                                modifier = Modifier
                                    .align(Alignment.Center)
                                    .padding(2.dp)
                                    .wrapContentSize()
                            )
                        }
                        LaunchedEffect(key1 = viewModel.pageIndex, block = {
                            "加载更多数据".log()
                            delay(1000)
                            viewModel.loadMoreData(channelEntity.cname)
                        })
                    }
                }
            })
        })
    }
}

//  if (!uiState.value.datas.isNullOrEmpty() && uiState.value.status != RequestDataStatus.LoadMoreIng) {
//
//                        //加载更多
//                        item {
//                            Box(
//                                modifier = Modifier
//                                    .fillMaxWidth()
//                                    .align(Alignment.Center)
//                            ) {
//                                Text(
//                                    text = "加载中...",
//                                    modifier = Modifier
//                                        .align(Alignment.Center)
//                                        .padding(2.dp)
//                                        .wrapContentSize()
//                                )
//                            }
//                            LaunchedEffect(key1 = viewModel.pageIndex, block = {
//                                delay(1000)
//                                viewModel.loadMoreData(channelEntity.cname)
//                            })
//                        }
//                    }